package com.ic.loan.activity

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.*
import com.gyf.immersionbar.ktx.immersionBar
import com.ic.loan.R
import com.ic.loan.bean.MyInfo
import com.ic.loan.bean.MyWallet
import com.ic.loan.cache.Constant
import com.ic.loan.http.ApiUrl
import com.ic.loan.http.HttpUtils
import com.ic.loan.http.ResultListener
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.jessyan.autosize.AutoSizeConfig
import me.jessyan.autosize.internal.CancelAdapt
import me.jessyan.autosize.unit.Subunits

class SplashActivity : BaseActivity(),CancelAdapt {

    override fun onCreate(savedInstanceState: Bundle?) {
        immersionBar {
            statusBarDarkFont(true)
        }
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)

        AutoSizeConfig.getInstance().unitsManager
            .setSupportDP(false)
            .setSupportSP(false).supportSubunits = Subunits.MM
        lifecycleScope.launch(Dispatchers.Default){
            delay(1000)
            withContext(Dispatchers.Main){
                when {
                    !SPUtils.getInstance().getBoolean(Constant.AGREEMENT) -> {
                        ActivityUtils.startActivity(UserAgreementActivity::class.java)
                        onBackPressed()
                    }
                    SPUtils.getInstance().getString(Constant.TOKEN).isNullOrBlank() -> {
                        ActivityUtils.startActivity(LoginActivity::class.java)
                        onBackPressed()
                    }
                    else -> {
                        getStatus()
                    }
                }
            }
        }
    }

    private fun getStatus() {
        HttpUtils.postOkGo(
            mutableMapOf("timestamp" to System.currentTimeMillis() / 1000),
            ApiUrl.POST_USER_PROFILE,
            object : ResultListener() {
                override fun onSucceeded(json: String?) {
                    val myInfo = GsonUtils.fromJson(json, MyInfo::class.java)
                    when {
                        myInfo.balance.toDouble() > 0.0 -> {
                            ActivityUtils.startActivity(MainActivity::class.java)
                            onBackPressed()
                        }
                        myInfo?.fullname.isNullOrBlank() -> {
                            if (SPUtils.getInstance().getInt(Constant.APPLY_SUCCESS) == 1){
                                ActivityUtils.startActivity(MyInformationActivity::class.java)
                            }else{
                                ActivityUtils.startActivity(StartActivity::class.java)
                            }
                            onBackPressed()
                        }
                        else -> {
                            //获取钱包数据
                            updateApproval()
                        }
                    }
                }

                override fun onFailed(content: String?) {
                    super.onFailed(content)
                    ActivityUtils.startActivity(LoginActivity::class.java)
                    onBackPressed()
                }


                override fun onLoadFinish() {
                }
            })
    }

    private fun updateApproval() {
        HttpUtils.postOkGo(mutableMapOf("timestamp" to System.currentTimeMillis() / 1000),
            ApiUrl.POST_USER_MYWALLET,object : ResultListener() {
                override fun onSucceeded(json: String?) {
                    val myWallet = GsonUtils.fromJson(json, MyWallet::class.java)
                    if (myWallet.app_amount.toDouble() > 0){
                        ActivityUtils.startActivity(MainActivity::class.java)
                        onBackPressed()
                    }else{
                        ActivityUtils.startActivity(WhyChooseUsActivity::class.java)
                        onBackPressed()
                    }
                }
            })
    }


}
